package com.cskaoyan.shopping.controller;

import com.alibaba.fastjson.JSON;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.shopping.dto.CartProductDto;
import com.cskaoyan.shopping.form.CartRequest;
import com.cskaoyan.shopping.form.UserInfo;
import com.cskaoyan.shopping.service.ICartService;
import com.netflix.client.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author ZhangHang @latimer.top
 * @version 1.0.0
 * @description TODO
 * @date 2022/12/3 9:25
 */
@RestController
public class ICartController {
    @Autowired
    ICartService cartService;

    @DeleteMapping("/shopping/carts/{uid}/{pid}")
    public ResponseData deleteItem(@PathVariable("uid") Long userId, @PathVariable("pid") Long productId) {
        cartService.deleteCartItem(userId, productId);
        return new ResponseUtil<String>().setData("成功", "success");
    }

    /*
     *@description TODO 查看购物车商品
     *@param []
     *@return com.cskaoyan.mall.commons.result.ResponseData
     *@date 2022/12/3 10:11
     *@author liyangyang @latimer.top
    */
    @GetMapping("/shopping/carts")
    public ResponseData getCarts(HttpServletRequest request){
        String user_info = request.getHeader("user_info");
        UserInfo userInfo = JSON.parseObject(user_info, UserInfo.class);
        List<CartProductDto> result = cartService.getCartListById(userInfo.getUid());
        return new ResponseUtil<>().setData(result);
    }

    /*
     *@description TODO 添加购物车
     *@param []
     *@return com.cskaoyan.mall.commons.result.ResponseData
     *@date 2022/12/3 10:19
     *@author liyangyang @latimer.top
    */
    @PostMapping("/shopping/carts")
    public ResponseData postCarts(@RequestBody CartRequest request){
        cartService.addToCart(request);
        return new ResponseUtil<>().setData(null);
    }

    /*
     *@description TODO 修改购物车状态
     *@param [request]
     *@return com.cskaoyan.mall.commons.result.ResponseData
     *@date 2022/12/3 14:45
     *@author liyangyang @latimer.top
    */
    @PutMapping("/shopping/carts")
    public ResponseData putCarts(@RequestBody CartRequest request){
        cartService.updateCartNum(request);
        return new ResponseUtil<>().setData(null);
    }
}
